package cn.afactory.tygz.ui.fragments;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.lidroid.xutils.util.LogUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;
import com.squareup.leakcanary.RefWatcher;

import cn.afactory.tygz.TianYaGongZaiApplication;
import cn.afactory.tygz.common.AppDebug;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshGridView;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.afactory.tygz.ui.base.utils.DialogUtil;
import cn.afactory.tygz.ui.base.utils.LoadingDialog;
import cn.afactory.tygz.ui.base.utils.NoLeakHandler;

public abstract class BaseFragment extends Fragment implements
		OnFragmentSelectListener, NoLeakHandler.HandlerCallback {

	protected Context mContext;
	protected Activity mActivity;

	protected NoLeakHandler mUIHandler;

	protected LoadingDialog mLoadingDialog;
	
    protected ListView listView;
    protected PullToRefreshGridView plgridView;
    protected PullToRefreshListView plistview;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		mUIHandler = new NoLeakHandler(this);
		LogUtils.e("Fragment is " + this);
		
	}


    private void applyScrollListener() {
        ImageLoader imageLoader = ImageLoader.getInstance();
        if (listView != null) {
            listView.setOnScrollListener(new PauseOnScrollListener(imageLoader,
                    true, true, getOnScrollListener()));
        }
        if (plgridView != null) {
            plgridView.setOnScrollListener(new PauseOnScrollListener(
                    imageLoader, true, true));
        }
        if (plistview != null) {
            plistview.setOnScrollListener(new PauseOnScrollListener(
                    imageLoader, true, true));
        }
    }
    

    protected AbsListView.OnScrollListener getOnScrollListener() {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.pajk.hm.sdk.android.util.NoLeakHandler.HandlerCallback#handleMessage(android
     * .os.Message)
     * 
     * 重写此方法来使用uihandler
     */
	@Override
	public void handleMessage(Message msg) {
		Log.d("BaseFragment", "handleMessage===>" + msg);
	}

	public void showLoadingView(String msg) {
		if(mContext == null){
			return ;
		}
		if (mLoadingDialog == null) {
			mLoadingDialog = DialogUtil.showLoadingDialog(mContext, msg, true);
		}
		mLoadingDialog.setDlgMessage(msg);
		if (!mLoadingDialog.isShowing()) {
			mLoadingDialog.show();
		}
	}

	public void hideLoadingView() {
		if(mLoadingDialog == null){
			return ;
		}
		try {
			if (mLoadingDialog.isShowing()) {
				mLoadingDialog.dismiss();
			}
		} catch (Exception e) {
		}
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mContext = activity;
		mActivity = activity;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
	    applyScrollListener();
		super.onResume();
	}

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
	}

	@Override
	public void onDestroy() {
		if (mUIHandler != null) {
			mUIHandler.setValid(true);
		}
		//内容泄露检测
		if(AppDebug.MEMEORY_LEAK_CAP_DEBUG) {
			RefWatcher refWatcher = TianYaGongZaiApplication.getRefWatcher(getActivity());
			refWatcher.watch(this);
		}
		super.onDestroy();
	}

	@Override
	public void onSelected(boolean select) {
	}

}
